package com.flow.asyncflow.core.entity.autooperation;


import lombok.SneakyThrows;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class PlaywrightBase {

//    private static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
//    //private static Playwright playwright;
//    //private static Browser browser;
//    private static HashMap<String, Playwright> playwrightMap = new HashMap<>();
//    private static HashMap<String, Browser> browserMap = new HashMap<>();
//    private static HashMap<String, BrowserContext> browserContextMap = new HashMap<>();
//    private static HashMap<String, Page> pageMap = new HashMap<>();
//
//    @SneakyThrows
//    public static Page getPage(AutoOperationContext autoOperationContext) {
//        Page page;
//        BrowserContext browserContext;
//        String uniqueNumber = autoOperationContext.getOperator() + "_" + autoOperationContext.getOperatorAccount() + "_" + autoOperationContext.getCountry();
//        page = pageMap.get(uniqueNumber);
//        if (page == null) {
//            //init(autoOperationContext);
//            //getPlaywright();
//            //getBrowser(autoOperationContext);
//            browserContext = getBrowserContext(autoOperationContext);
//
//            page = browserContext.newPage();
//            page.setDefaultTimeout(autoOperationContext.getPlaywrightInfo().getTimeOut());
//            pageMap.put(uniqueNumber, page);
//            //过期10分钟
//            setExpiration(autoOperationContext, 600000);
//        }
//        return page;
//    }
//
//    @SneakyThrows
//    public static BrowserContext getBrowserContext(AutoOperationContext autoOperationContext) {
//        String uniqueNumber = autoOperationContext.getOperator() + "_" + autoOperationContext.getOperatorAccount() + "_" + autoOperationContext.getCountry();
//
//        BrowserContext browserContext = browserContextMap.get(uniqueNumber);
//
//        if (browserContext == null) { //创建“隐身”浏览器上下文
//            Browser browser = getBrowser(autoOperationContext);
//            browserContext = browser.newContext(
//                    new Browser.NewContextOptions()
//                            .setIgnoreHTTPSErrors(true)
//                            .setJavaScriptEnabled(true)
//                            .setViewportSize(null));
//            browserContextMap.put(uniqueNumber, browserContext);
//            //过期10分钟
//            setExpiration(autoOperationContext, 600000);
//        }
//        return browserContext;
//    }
//
//
//    @SneakyThrows
//    public static Browser getBrowser(AutoOperationContext autoOperationContext) {
//        String uniqueNumber = autoOperationContext.getOperator() + "_" + autoOperationContext.getOperatorAccount() + "_" + autoOperationContext.getCountry();
//        Browser browser = browserMap.get(uniqueNumber);
//        if (browser == null) {
//            Playwright initPlaywright = getPlaywright(autoOperationContext);
//            //String server = ConfigCenterUtils.getOtherProject("tcgl.visa.admin.sys", "VisaProxUrl") + ":" + ConfigCenterUtils.getOtherProject("tcgl.visa.admin.sys", "VisaProxPort");
//            Proxy proxy = null;
//            String server = "10.181.172.132:1080";
//            if (autoOperationContext.getPlaywrightInfo().getProxyInfo() != null
//                    && StringUtils.isNotBlank(autoOperationContext.getPlaywrightInfo().getProxyInfo().getHostName())) {
//                server = autoOperationContext.getPlaywrightInfo().getProxyInfo().getHostName() + ":" + autoOperationContext.getPlaywrightInfo().getProxyInfo().getPort();
//                proxy = new Proxy(server);
//                proxy.setUsername(autoOperationContext.getPlaywrightInfo().getProxyInfo().getUserName());
//                proxy.setPassword(autoOperationContext.getPlaywrightInfo().getProxyInfo().getPassWord());
//            }
//
//            List<String> args = new ArrayList<>();
//            args.add("--start-maximized");
//            args.add("--incognito");
//            args.add("--disable-blink-features=AutomationControlled");
//            BrowserType.LaunchOptions launchOptions = new BrowserType.LaunchOptions()
//                    .setHeadless(autoOperationContext.getPlaywrightInfo().isHeadless())
//                    .setArgs(args);
//            if (proxy != null) {
//                launchOptions.setProxy(proxy);
//            }
//            browser = initPlaywright.chromium().launch(launchOptions);
//            browserMap.put(uniqueNumber, browser);
//            //过期10分钟
//            setExpiration(autoOperationContext, 600000);
//        }
//        return browser;
//    }
//
//    @SneakyThrows
//    public static Playwright getPlaywright(AutoOperationContext autoOperationContext) {
//        String uniqueNumber = autoOperationContext.getOperator() + "_" + autoOperationContext.getOperatorAccount() + "_" + autoOperationContext.getCountry();
//        Playwright playwright = playwrightMap.get(uniqueNumber);
//        if (playwright == null) {
//            playwright = Playwright.create();
//            playwrightMap.put(uniqueNumber, playwright);
//        }
//        return playwright;
//    }
//
//    public static void retryInit(AutoOperationContext autoOperationContext) {
//        String uniqueNumber = autoOperationContext.getOperator() + "_" + autoOperationContext.getOperatorAccount() + "_" + autoOperationContext.getCountry();
//        browserContextMap.put(uniqueNumber, null);
//        pageMap.put(uniqueNumber, null);
//    }
//
//    //@SneakyThrows
//    //private static void init(AutoOperationContext autoOperationContext) {
//    //    Page page;
//    //    String uniqueNumber = autoOperationContext.getOperator() + "_" + autoOperationContext.getOperatorAccount() + "_" + autoOperationContext.getCountry();
//    //    if (playwright == null) {
//    //        playwright = Playwright.create();
//    //    }
//    //    getBrowser(autoOperationContext);
//    //}
//
//    public static void delPage(AutoOperationContext autoOperationContext) {
//        String uniqueNumber = autoOperationContext.getOperator() + "_" + autoOperationContext.getOperatorAccount() + "_" + autoOperationContext.getCountry();
//        Browser browser = browserMap.get(uniqueNumber);
//        BrowserContext browserContext = browserContextMap.get(uniqueNumber);
//        Page page = pageMap.get(uniqueNumber);
//        if (page != null) {
//            page.close();
//        }
//        if (browserContext != null) {
//            browserContext.close();
//        }
//        if (browser != null) {
//            browser.close();
//        }
//        browserMap.remove(uniqueNumber);
//        browserContextMap.remove(uniqueNumber);
//        pageMap.remove(uniqueNumber);
//    }
//
//    private static void setExpiration(AutoOperationContext autoOperationContext, long delay) {
//        scheduler.schedule(() -> {
//            delPage(autoOperationContext);
//            // 代码执行对象过期时的操作
//        }, delay, TimeUnit.MILLISECONDS);
//    }
}
